<?php
if(isset($_REQUEST["action"]) && $_REQUEST["action"]=="send")
{
	require('../connect_db.php');
	
	$subject = trim($_POST['psubject']);
	$body = trim($_POST['pbody']);
	
	$receivers_result = mysql_query("select email FROM dummyusers");
	while($receiver_row = mysql_fetch_array($receivers_result))
	{
		SendMail("Hier de naam uit database", trim($receiver_row["email"]), $subject, $body);
	}
	
	header("Location: " . $_SERVER["PHP_SELF"] . "?action=done");
}
function SendMail ($toname, $toemail, $subject, $body)
{
// TIP: verplaats email adres naar een configuratie tabel of een configuratie include(php file)
// TIP: Gebruik header om ook de naam van de versturende partij weer te geven: $header .= "From: Deli <" . $frommail . ">\r\n";
// TIP: Gebruik header om ook de naam van de ontvangende partij weer te geven (TO)
	$frommail ="turkishdely@fromindia.be";

	$body = str_replace("[ontvanger]", $toname, $body);
	
	$headers = "From: " . strip_tags($frommail) . "\r\n";
	$headers .= "Reply-To: ". strip_tags($frommail) . "\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

	$message = "<html><body>";
	$message .= $body;
	$message .= "</body></html>";

    mail($toemail, $subject, $message, $headers);
}
?>

<html>
<head>	
<title>Mailert</title>
</head>
<body>

<?php

if(!isset($_REQUEST["action"]))
{
	?>
		<!-- Include the WYSIWYG javascript files -->
		<script type="text/javascript" src="scripts/wysiwyg.js"></script>
		<script type="text/javascript" src="scripts/wysiwyg-settings.js"></script>

		<!-- Attach the editor on the textareas -->
		<script type="text/javascript">
			WYSIWYG.attach('pbody'); // default setup
		</script>

		<!-- TIP: Mooier maken van de pagina -->
		<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>?action=send">
		<h1>Sturen die shizzel</h1>
		<table>
				<td>Subject:</td>
				<td><input type="text" id="psubject" name="psubject" value="subjectje" /></td>
			</tr>
			<tr>
				<td>Body:</td>
				<!-- Vervangen door simpele wysiwyg editor -->
				<td><textarea id="pbody" name="pbody" style="width:560px;height:200px;"></textarea></td>		
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input type="submit" id="sendbutton" value="Spam!" />
			</tr>
			
		</table>
	</form>

	<?php
	}
	else if($_REQUEST["action"]=="done")
	{
	// TIP: maak pagina met duidelijke tekst, mail is verstuur naar x aantal gebruikers bijvoorbeeld
	 // done sending the freaking mail
	 ?>
	 <h1>Ready!</h1>
	 <?php
	}
		
	?>

</body>
</html>